# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify
from flask_cors import CORS
import json
from mryxService import *


app = Flask(__name__)
CORS(app)


def getParams(reqs, key):
    try:
        vals = reqs.form[key].encode("utf-8").decode("utf-8")
        if vals and len(vals) > 0:
            return vals
    except Exception:
        return


@app.route('/doservice', methods=["POST"])
def doservice():
    fsCaName = None
    secCaName = None
    respData = None
    commentNum = None
    try:
        fsCaName = getParams(request, 'fsCaName')
        secCaName = getParams(request, 'secCaName')
        commentNum = getParams(request, 'commentNum')
        respData = json.loads(getParams(request, 'respData'))
    except Exception:
        pass
    if fsCaName and secCaName and respData:
        status = parser(respData, fsCaName, secCaName, commentNum)
        if status:
            resp = {"status": "true", "data": "解析成功"}
        else:
            resp = {"status": "false", "data": "解析失败"}
    else:
        resp = {"status": "false", "data": "获取失败"}
    return jsonify(resp)


if __name__ == '__main__':
    app.config['JSON_AS_ASCII'] = False
    app.run(host='0.0.0.0', debug=False, port=8789)
    CORS(app)
